<?php
namespace app\front\service\v2;
use app\front\service\v2\Redis;
use app\front\model\Store;
use app\front\model\Shop as ShopModel;
use app\front\model\Award as AwardModel;
use app\front\service\GetMsgService;
class Show extends GetMsgService
{
    private static $_instance;
    
    //private 构造函数
    private function __construct() {
       // echo " I'm construct! process id is " . getmypid() . " and thread id is " . Thread::getCurrentThreadId() . "\n";
    }
    
    private function __clone() {
    //    echo " I'm clone! process id is " . getmypid() . " and thread id is " . Thread::getCurrentThreadId() . "\n";
    }
    
    //单例方法访问类实例
    public static function getInstance() {
        if (!(self::$_instance instanceof self)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }  
    
    
    public function ajaxShowStore(){
        $keyword = input('param.keyword');
        $where['store_name']=array('like','%'.$keyword.'%');//写查询条件
        $slist=  Store::where($where)->where('status is NULL')->field('store_name')->select();
        $slist= json_decode(json_encode($slist),true);
        $list=more_array_unique($slist);
        echo json_encode($list);
    }
    
    public function distance($dis){
        $redis=Redis::getInstance();
        session(session_id().'dis',$dis);
      //  logs("TIME.TXT", "START".date("Y-m-d H:i:s",time()));
        $redis->delete("stores".session_id());
        if(!$redis->exists("stores".session_id())){
            $redis->delete("zfshlmStoreData");
            if(!$redis->exists("zfshlmStoreData")){
                $store_data = Store::table('store')->where('status is NULL')->where('position is not NULL')->select();
                $store_data =json_decode(json_encode($store_data),true);
                $redis->rPush("zfshlmStoreData",json_encode($store_data));
                $redis->setTimeout("zfshlmStoreData",3600);
            }else{
                $store_data=json_decode($redis->lget("zfshlmStoreData",0),true);
            }
           // logs("TIME.TXT", "CENTER".date("Y-m-d H:i:s",time()));
            for($i = 0;$i<count($store_data);$i++){
                $store_data[$i]['distance'] = getDistance($dis,$store_data[$i]['position'],1);//json_decode($result,true)['results'][0]['distance'];
                $store_data[$i]['prob']=$redis->hget("shopid", 'shopid'.$store_data[$i]['shop_id']);//$this->getawardForshopid( $store_data[$i]['shop_id']);
            }
         //   logs("TIME.TXT", "END1".date("Y-m-d H:i:s",time()));
            $distance = array();
            foreach ($store_data as $store) {
                $distance[] = $store['distance'];
            }
            array_multisort($distance, SORT_ASC, $store_data);
           // logs("STOREDATA.TXT",  $store_data);
            for($i = 0;$i<count($store_data);$i++){
                $redis->rPush("stores".session_id(),json_encode($store_data[$i]));
            }
            $redis->setTimeout("stores".session_id(),3600);
            //logs("TIME.TXT", "END2".date("Y-m-d H:i:s",time()));
        }
        echo json_encode($redis->lrange("stores".session_id(),0,-1));
        
    }
    
    public function distanceTrade(){
        $dis=  session(session_id().'dis');
        $trade_id= input('param.trade_id');
        $redis=Redis::getInstance();
        //  $redis->delete("stores".session_id().$trade_id);
        if(!$redis->exists("stores".session_id().$trade_id)){
            //    $redis->delete("zfshlmStoreData".$trade_id);
            if(!$redis->exists("zfshlmStoreData".$trade_id)){
                $store_data = Store::table('store')->where('status is NULL')->where('position is not NULL')->where('trade_id',$trade_id)->select();
                $store_data =json_decode(json_encode($store_data),true);
                $redis->rPush("zfshlmStoreData".$trade_id,json_encode($store_data));
                $redis->setTimeout("zfshlmStoreData".$trade_id,3600);
            }else{
                $store_data=json_decode($redis->lget("zfshlmStoreData".$trade_id,0),true);
            }
            
            for($i = 0;$i<count($store_data);$i++){
                $store_data[$i]['distance'] = getDistance($dis,$store_data[$i]['position'],1);//json_decode($result,true)['results'][0]['distance'];
                $store_data[$i]['prob']=$redis->hget("shopid", 'shopid'.$store_data[$i]['shop_id']);//$this->getawardForshopid( $store_data[$i]['shop_id']);
            }
            
            $distance = array();
            foreach ($store_data as $store) {
                $distance[] = $store['distance'];
            }
            array_multisort($distance, SORT_ASC, $store_data);
            for($i = 0;$i<count($store_data);$i++){
                $redis->rPush("stores".session_id().$trade_id,json_encode($store_data[$i]));
            }
            $redis->setTimeout("stores".session_id().$trade_id,600);
        }
        
        echo json_encode($redis->lrange("stores".session_id().$trade_id,0,-1));
    }
    
    
    public function distanceStore($store_name){
        $redis=Redis::getInstance();
        $dis=  session(session_id().'dis');
        $store_data = Store::table('store')->where('status is NULL')->where('store_name',$store_name)->select();
        
        for($i = 0;$i<count($store_data);$i++){
            $store_data[$i]['distance'] = getDistance($dis,$store_data[$i]['position'],1);//json_decode($result,true)['results'][0]['distance'];
            $store_data[$i]['prob']=$redis->hget("shopid", 'shopid'.$store_data[$i]['shop_id']);//$this->getawardForshopid( $store_data[$i]['shop_id']);
        }
        for($i = 0;$i<count($store_data);$i++){
            
            $distance[] = $store_data[$i]['distance'];
            
        }
        $distance = array();
        foreach ($store_data as $store) {
            $distance[] = $store['distance'];
        }
        array_multisort($distance, SORT_ASC, $store_data);
        return $store_data;
    }
    

    /**
     * 函数用途描述 :根据商户ID获取其获奖概率
     * @date: 2018年5月29日 下午12:07:26
     * @author: 孙洪亮
     * @param: $shop_id
     * @return: $prob
     */
    public function getawardForshopid($shop_id){
        //获取分公司ID
        $filiale_id=ShopModel::where('id',$shop_id)->field('filiale_id')->find();
        //判断award表中是否设置了特定概率（走的是不是默认概率）
        if(!AwardModel::where('filiale_id',$filiale_id['filiale_id'])->find()){
            //没设置走默认
            $prob= AwardModel::where('filiale_id',0)->field('prob')->find()['prob'];
        }else{
            //设置了走设置
            $prob= AwardModel::where('filiale_id',$filiale_id['filiale_id'])->field('prob')->find()['prob'];
        }
        //返回概率
        return $prob;
    }
    
    public function UpdateAwardForShopid(){
        $redis=Redis::getInstance();
        $redis->del("shopid");
        $shopdata=ShopModel::where('status is null')->select();
        $shopdata=json_decode(json_encode($shopdata),true);
        for ($i=0;$i<count($shopdata);$i++){
            $redis->hset("shopid", 'shopid'.$shopdata[$i]['id'], $this->getawardForshopid($shopdata[$i]['id']));
        }
        dump($redis->hgetall("shopid")) ;  //获取hash中所有的键值对
        echo $redis->hlen("shopid");     //获取hash中键值对的个数
        // echo   $redis->hget("shopid", 'shop1');
    }
}


